local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local event_manager = _G.ClientOHOH.event_manager;
local EventList = _G.ClientOHOH.load.EventList();
local CodeAnimationTool=_G.CommonOHOH.load.CodeAnimationTool();
local GeneralTool=_G.CommonOHOH.load.GeneralTool();

local EffectId={
    base_meat="Effect.MeatPerfab",
    base_wins="Effect.winsPerfab",
    base_diamond="Effect.diamondPerfab",
}

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.player=self.parent.player
	self.weight=self.parent.weight
	self.drop=self.parent.drop
	self.ItemId2Idex={}
	for k,v in pairs(self.drop) do
		for key,count in pairs(v) do
			self.ItemId2Idex[key]=k
		end
	end
	self.percent=GeneralTool.GetProbabilityShowPercent(self.weight)
	self.HomeCtrl=self.player:GetComponent(_G.ClientOHOH.load.Client_HomeCtrl())


	-- print("--------- self.cfg--------", self.parent.cfg.id)
	local att=self.parent:WaitForChild_Loop("Shoot.DieBoom")
	self.BoomPos=att.WorldCFrame.Position
	self.BoomCfg={
		radius=att:GetAttribute("radius"),
		height=att:GetAttribute("height"),
		time=att:GetAttribute("time"),
	}

end;


function M.GetPercentStr(self,itemId)
	 local index= self.ItemId2Idex[itemId]
	 return math.floor(self.percent[index]).."%"
end

local ItemTool=_G.CommonOHOH.load.ItemTool();
local UIManager = _G.ClientOHOH.load.UIManager();
local WeaponTool=_G.CommonOHOH.load.WeaponTool();

function M.Drop(self,drop)
	for k,v in pairs(drop) do
		local key,id=ItemTool.GetKeyAndID(k)
		if EffectId[k] then
			self:PlayEffect(k,v,6)
		elseif key=="weapon" then
			local data=WeaponTool.Calculate(id)
			UIManager.ui_list.WeaponDropEffectGUi:OnShow(data.icon,self:GetPercentStr(k),data.name);
		else
			ItemTool.FlyingItem(k,v)
		end
	end
end

function M.PlayEffect(self,itemId,count,effectCount)
   local sum=effectCount
   local posList={
		[itemId]={},
	}

	CodeAnimationTool.Drop3DAnimation.Play(function()
		return _G.POOL:GetObj(EffectId[itemId])
	end,function(e)
		table.insert(posList[itemId],e.Position)
		_G.POOL:PushObj(EffectId[itemId],e)
		sum=sum-1
		if sum==0 then
			self.HomeCtrl:AddBaseItem(itemId,posList[itemId],count)
		end

	end,sum,self.BoomPos,self.BoomCfg)
end

event_manager:RegistHandle(EventList.trian_drop, function(player,guid,dropData)

	local compt = player:GetComponent(_G.CommonOHOH.load.Common_TrainCtrl());
	if compt then
		local trianObj=compt:FindTrianObj(guid);
		local dropCtrl=trianObj:GetPrivateComponent("drop")
		dropCtrl:Drop(dropData)
	end;
end)


return M